import {combineReducers} from 'redux-immutable';
import Immutable from 'immutable';
import * as actions from './actions';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION, HISTORY_DEFAULT_QUERY_OPTION} from './constants';
import {subClaimApplyStatus} from '../Enum';

const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                brands: action.data.brands,
                partSaleOrderTypes: action.data.partSaleOrderTypes,
                enterprise: action.data.enterprise,
                isDealerUser: action.data.isDealerUser,
                user: action.data.user,
            });
        case actions.GET_INIT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_BEGIN:
            return state.merge({
                isFetching: true,
            });
        case actions.GET_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                data: action.data.content
            });
        case actions.GET_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                data: []
            });
        default:
            return state;
    }
};
const historyList = (state, action) => {
    switch(action.type) {
        case actions.GET_HISTORY_LIST_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0
            });
        case actions.GET_HISTORY_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_HISTORY_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message || '',
                data: []
            });
        default:
            return state;
    }
};
const appState = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.setIn(['queryCondition', action.name], action.value);
        case actions.GET_INIT_DATA_SUCCESS:
            if(!action.data.enterprise)
                return state.setIn(['queryCondition', 'status'], [subClaimApplyStatus.待审核]);
            return state;
        case actions.RESET_QUERY_PANEL:
            return state.set('queryCondition', Immutable.fromJS(DEFAULT_QUERY_OPTION));
        case actions.GET_LIST_SUCCESS:
            return state.set('queryBySort', Immutable.fromJS(action.condition));
        case actions.SAVE_QUERY_DEALER:
            return state.set('queryDealer', action.data);
        case actions.SELECT_HISTORY_DEALER_QUERY:
            return state.set('queryHistoryDealer', action.data);
        case actions.SAVE_HISTORY_QUERY_CONDITION:
            return state.setIn(['historyQueryCondition', action.name], action.value);
        case actions.RESET_HISTORY_QUERY_PANEL: {
            return state.set('historyQueryCondition', Immutable.fromJS(HISTORY_DEFAULT_QUERY_OPTION));
        }
        case actions.GET_HISTORY_LIST_SUCCESS:
            return state.set('historyQueryBySort', Immutable.fromJS(action.condition));
        case actions.SAVE_QUERY_FAULTITEM:
            return state.set('queryFaultItem', action.data);
        default:
            return state;
    }
};
const category = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_CATEGORY_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_CATEGORY_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data.content
            });
        case actions.GET_INIT_CATEGORY_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};
const showImg = (state, action) => {
    switch(action.type) {
        case actions.UPDATE_SHOWIMG:
            return state.merge({
                index: action.data
            });
        default:
            return state;
    }
};
const domainData = combineReducers({
    permission,
    initData,
    list,
    historyList,
    category
});

export default combineReducers({
    domainData,
    appState,
    showImg
});
